<template>
  <el-dialog title="用户选择" :visible.sync="visible" :close-on-click-modal="false" append-to-body width="1000px">
    <div class="app-container">
      <!-- 条件搜索 -->
      <div class="filter-container">
        <el-col :span="5">
          <ft-org-select v-model="listQuery.param.orgId" :placeholder="'组织名称'" :multiple="false" :default-expand-level="2" />
        </el-col>
        <el-col :span="5">
          <el-input v-model="listQuery.param.username" placeholder="用户名" style="width: 100%;" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-col>
        <el-col :span="5">
          <el-input v-model="listQuery.param.mobile" placeholder="手机号" style="width: 100%;" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-col>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilterQuery">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" icon="el-icon-refresh-right" @click="handleRestQuery">
          重置
        </el-button>
      </div>
      <div class="filter-container">
        <div style="float: right;">
          <el-button icon="el-icon-refresh" circle @click="handleFilterQuery" />
        </div>
      </div>

      <!-- 数据表格 -->
      <el-table
        ref="userTable"
        :key="tableKey"
        v-loading="loading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        scrollbar-always-on="true"
        @current-change="handleCurrentChange"
      >
        <el-table-column label="序号" type="index" width="50px" align="center" />
        <el-table-column label="用户ID" min-width="80px" align="left">
          <template v-slot="{row}">
            <span>{{ row.userId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用户名" min-width="90px" align="left">
          <template v-slot="{row}">
            <span>{{ row.username }}</span>
          </template>
        </el-table-column>
        <el-table-column label="姓名" min-width="120px" align="left">
          <template v-slot="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号" min-width="110px" align="left">
          <template v-slot="{row}">
            <span>{{ row.mobile }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属组织" min-width="100px" align="left">
          <template v-slot="{row}">
            <span>{{ row.orgName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="账户状态" min-width="70px" align="left">
          <template v-slot="{row}">
            <el-tag v-if="row.locked === 'Y'" type="info">锁定</el-tag>
            <el-tag v-else type="success">正常</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="使用状态" class-name="status-col" width="70px">
          <template v-slot="{row}">
            <el-switch :value="row.status ==='Y'" disabled />
          </template>
        </el-table-column>
      </el-table>

      <!-- 表格分页 -->
      <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="handleInit" />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button icon="el-icon-close" @click="handleClose">
        取消
      </el-button>
      <el-button type="primary" icon="el-icon-check" @click="handleConfirmSelection">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { handlePageList } from '@/views/sys/userInfo/api'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'
import FtOrgSelect from '@/views/components/FtOrgSelect/index.vue'

export default {
  // 当前页面名称（组件）
  name: 'FtUserDialog',
  // 引用组件
  components: { Pagination, FtOrgSelect },
  directives: { waves },
  filters: {},
  // props属性 这里只定义了基本的，目的是防止用户传递无效的属性（如错误拼写），其他的el-select的属性会默认继承，用法一样，因为使用v-bind="$attrs"，这个会将非props定义的属性继承过来
  props: {
    // 初始选中的用户
    value: {
      type: Object,
      default: () => ({})
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 列表table的key
      tableKey: 0,
      // 遮罩层
      loading: true,
      // table数据对象
      list: [],
      // 总条数
      total: 0,
      // table条件查询对象
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        // sort: '+id',
        param: {
          username: undefined,
          mobile: undefined,
          orgId: undefined,
          locked: undefined,
          status: undefined
        }
      },
      // 是否显示弹出层
      visible: false,
      // 选中值
      selectedValue: {}
    }
  },
  computed: {},
  watch: {
    // 监听外部传入的value变化
    value: {
      handler(newVal) {
        this.selectedValue = newVal || {}
      },
      deep: true,
      immediate: true
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 打开弹窗
    open() {
      this.visible = true
      this.$nextTick(() => {
        this.handleInit()
      })
    },
    // 查询table数据对象
    handleInit() {
      this.loading = true
      handlePageList(this.listQuery).then(res => {
        this.list = res.data
        if (res.total) {
          this.total = Number(res.total)
        }
        this.loading = false
      })
    },
    // 条件查询table数据
    handleFilterQuery() {
      this.listQuery.pageNum = 1
      this.handleInit()
    },
    // 重置table查询条件
    handleRestQuery() {
      this.listQuery.param.orgId = undefined
      this.listQuery.param.username = undefined
      this.listQuery.param.mobile = undefined
    },
    handleCurrentChange(val) {
      this.selectedValue = val
    },
    // 确认选择
    handleConfirmSelection() {
      if (!this.selectedValue.id) {
        this.$message.warning('请选择一行记录！')
        return
      }
      this.$emit('input', this.selectedValue)
      this.$emit('select', this.selectedValue)
      this.visible = false
    },
    // 关闭弹出层
    handleClose() {
      // 恢复之前选中的用户
      if (this.value && this.value.id) {
        this.selectedValue = this.value
      } else {
        this.selectedValue = {}
      }
      this.visible = false
    }
  }
}
</script>
